<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-点击修改元素背景颜色</title>
    <style>
        .container{
            width: 400px;
            height: 300px;
            margin: 50px auto;
            background:#ccc;
        }
    </style>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>

    <script type="text/babel">
        //类式组件
        class App extends React.Component{

            state = {
                bg: 'rgb(1,1,50)'
            }
            
            render(){
                return <div className="container" onClick={this.changeBg} style={{backgroundColor: this.state.bg}}></div>
            }

            changeBg = () => {
                this.setState({
                    bg: `rgb(${_.random(0,255)},${_.random(0,255)},${_.random(0,255)})`
                })
            }
        }

        //渲染
        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>